package top.chaileilei.zookeeper.zkclient;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

/**
 * @Description
 * @Time 2019/2/11
 * @Author CHAILEILEI
 * @Weblog www.chaileilei.top
 */
public class ZkClientDemo {

    private static ZkClient zkClient;

    static {
        zkClient = new ZkClient("119.27.189.174:2181",5000);
    }

    public static void main(String[] args) throws Exception {
        getChildrenDemo();
    }

    public static void createNodeDemo(){
        String path = "/zkClient/cnd";
        zkClient.createPersistent(path,true);
    }

    public static void getChildrenDemo() throws Exception {
        String path = "/zkClient/cnd2";
        zkClient.subscribeChildChanges(path, new IZkChildListener() {
            @Override
            public void handleChildChange(String s, List<String> list) throws Exception {
                System.out.println(s+" 's child changed, currentChilds:" + list);
            }
        });
        zkClient.createPersistent(path,true);
        System.out.println("Node " + path + " exists " + zkClient.exists(path));
        zkClient.writeData(path, new Integer(1));
        Object o = zkClient.readData(path);
        System.out.println(o);
        zkClient.writeData(path,"456");
        Object o2 = zkClient.readData(path);
        System.out.println(o2);
        Thread.sleep( 1000 );
        System.out.println(zkClient.getChildren(path));
        Thread.sleep( 1000 );
        zkClient.createPersistent(path+"/c1");
        Thread.sleep( 1000 );
        zkClient.delete(path+"/c1");
        Thread.sleep( 1000 );
        zkClient.delete(path);
        Thread.sleep( Integer.MAX_VALUE );


    }

}
